package com.example.mybus;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.mybus.dao.MicroDao;
import com.example.mybus.model.Micro;
import com.example.mybus.model.pmf.PMF;

public class MicroServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger("MicroServlet");
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			List<Micro> micros = MicroDao.getAll();
			req.setAttribute("micros", micros);
		} finally {
			pm.close();
		}
		
		req.getRequestDispatcher("/micro/micros.jsp").forward(req, resp);
		log.log(Level.INFO, "Redirect to micros.jsp");
	}
}
